package src.tree;

public class no110 {//平衡二叉树

    public boolean isBalanced(TreeNode root) {
        int res = getMaxDepth(root);
        return res == -1 ? false : true;
    }

    public int getMaxDepth(TreeNode node) {
        if (node == null) return 0;
        //如果子树已经不是AVL了，再记录高度没意义，直接返回-1
        int left = getMaxDepth(node.left);
        if (left == -1) return -1;
        int right = getMaxDepth(node.right);
        if (right == -1) return -1;

        int res;
        if (Math.abs(left - right) > 1) res = -1;
        else res = Math.max(left, right) + 1;
        return res;
    }
}
